<template>
  <!--车辆状态及责任经销商修改 只有经销商端-->
  <div>
    <TopFilter ref="topfilter" :list="topFilterList" :cols="4" labelWidth="100" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :onSyncTableData="tableChange"
      :isExportExcel="false"
      exportFileName="车辆状态及责任经销商修改.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <add :formData="formData" :maId="maId" @close="closeHandler" :handleType="handleType" />
    </Drawer>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import add from './add';
import { isEmpty } from '@/utils/utils';

import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { searchVinList, exportSearchVinList,exporturl } from '@/api/repair/batchEventManagement/carStatusAndDealer';
import { arrayObjDeWeight } from '@/utils';

export default {
  name: 'addItem',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '审核' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      visibleDetail: false,
      fetchApi: searchVinList,
      dialogTitle: '',
      fetchParams: { xhrAbort: false },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      maId: '',
      areaId: ''
    };
  },
  components: {
    add
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;

    this.initDelear();
  },
  methods: {
    async exportHandle() {
      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();
      await this.downHandleFile(exportSearchVinList, params, '车辆状态及责任经销商修改');
    },

    tableChange(data) {
      data.forEach((item, index) => {
        var isEdit = '否';

        if (!isEmpty(item['oldResDealer'] || !isEmpty(item['oldVinStatus']))) {
          isEdit = '是';
        }
        item.isEdit = isEdit;
      });
    },

    // 导出处理函数
    handleExportFun(row) {
      row.vinStatus = this.createDictText(row.vinStatus, 6040);
      row.oldVinStatus = this.createDictText(row.oldVinStatus, 6040);
      row.ifOta = this.createDictText(row.ifOta, 1004);

      let isEdit = '否';

      if (!isEmpty(row['oldResDealer'] || !isEmpty(row['oldVinStatus']))) {
        isEdit = '是';
      }
      row.isEdit = isEdit;
    },

    async initDelear() {
      this.getAllDelears(this.columns, ['resDealer', 'oldResDealer'], 'table');
    },

    async initSelect() {
      let res = await getAllUser();
      // 去重
      let dataList = arrayObjDeWeight(res.data, 'userId');

      this.columns.find(item => item.dataIndex === 'maBy').dictItems = dataList.map(x => ({
        text: x.employeeName,
        value: x.userId
      }));
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: props => {
            /*备注  目前暂没有车辆状态码 待后台确定之后进行修改*/
            let obj = props.row;
            let vinStatus = isEmpty(obj['vinStatus']) ? '' : obj['vinStatus'];
            let rst = false;
            // if (vinStatus=='40211002')
            // {
            //   rst = true
            // }
            /**/
            return (
              <div>
                <el-button size="mini" type="text" disabled={rst} onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },

        {
          title: '是否修改',
          dataIndex: 'isEdit',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props =>
          // {
          //   let row = props.row;
          //
          //   let isEdit = '否';
          //
          //   if (!isEmpty(row['oldResDealer'] || !isEmpty(row['oldVinStatus'])))
          //   {
          //     isEdit = '是'
          //   }
          //   return <span>{isEdit}</span>;
          // }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最新车辆状态',
          dataIndex: 'vinStatus',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(6040),
          render: props => {
            return <span>{this.createDictText(props.row.vinStatus, 6040)}</span>;
          }
        },
        {
          title: '原车辆状态',
          dataIndex: 'oldVinStatus',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(6040),
          render: props => {
            let oldVinStatus = isEmpty(props.row.oldVinStatus) ? '' : props.row.oldVinStatus;
            return <span>{this.createDictText(oldVinStatus, 6040)}</span>;
          }
        },
        {
          title: '最新责任经销商',
          dataIndex: 'resDealer',
          width: 150,
          sorter: true,
          dictItems: []
        },
        {
          title: '原责任经销商',
          dataIndex: 'oldResDealer',
          width: 150,
          dictItems: [],
          sorter: true
        },

        {
          title: '活动编号',
          dataIndex: 'maNo',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '活动主题',
          dataIndex: 'maTitle',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '是否OTA活动',
          dataIndex: 'ifOta',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          render: props => {
            return <span>{this.createDictText(props.row.ifOta, 1004)}</span>;
          }
        },
        {
          title: '主损件代码',
          dataIndex: 'partCode',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主损件名称',
          dataIndex: 'partName',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '下发日期',
          dataIndex: 'maDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动主题',
          fieldName: 'maTitle',
          placeholder: '活动主题...'
        },
        {
          type: 'RANGE_DATE',
          label: '活动开始日期',
          dateType: 'daterange',
          style: { minWidth: '200px' },
          fieldName: 'bDate|bEndDate'
        },
        {
          type: 'RANGE_DATE',
          label: '活动截止日期',
          dateType: 'daterange',
          style: { minWidth: '200px' },
          fieldName: 'eBeginDate|eDate'
        },
        {
          type: 'INPUT',
          label: '主损件编号',
          fieldName: 'partCode',
          placeholder: '主损件编号...'
        },
        {
          type: 'INPUT',
          label: '主损件名称',
          fieldName: 'partName',
          placeholder: '主损件名称...'
        },
        {
          type: 'SELECT',
          label: '车辆状态',
          fieldName: 'vinStatus',
          placeholder: '请选择',
          itemList: this.createDictList(6040)
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN...'
        }
      ];
    },
    isEdit(row) {},
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    detailHandle(row) {
      this.maId = row['maId'];
      this.handleType = 'show';
      this.showHandler(1);
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.maId = row['maId'];
      this.areaId = row['areaId'];
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      this.fetchParams = Object.assign({}, val);

      console.log(this.fetchParams);
    },
    showHandler(type = 0) {
      this.dialogTitle = `${this.conf[this.handleType]}车辆状态及责任经销商`;
      if (type == 0) {
        this.visible = true;
      } else {
        this.visibleDetail = true;
      }
    },
    changgeDictYN(val) {
      console.log(val);
      if (val === 10041001) {
        return '是';
      } else {
        return '否';
      }
    },
    closeHandler(type) {
      this.visible = false;
      this.visibleDetail = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
